import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
//课程表操作类
class CourseTableOperator {
    private Connection connection;

    public CourseTableOperator(Connection connection) {
        this.connection = connection;
    }

    public void createTable() throws SQLException {
        Statement statement = connection.createStatement();
        String sql = "CREATE TABLE IF NOT EXISTS course (" +
                "课程编码 INT PRIMARY KEY," +
                "课程名称 VARCHAR(255)," +
                "课程类型 VARCHAR(100)," +
                "课程学分 DECIMAL(5, 2)," +
                "开始学期 VARCHAR(50)" +
                ")";
        statement.executeUpdate(sql);
        statement.close();
    }

    public void insertCourse(int id, String name, String type, double credit, String semester) throws SQLException {
        String sql = "INSERT INTO course (课程编码, 课程名称, 课程类型, 课程学分, 开始学期) VALUES (?,?,?,?,?)";
        PreparedStatement statement = connection.prepareStatement(sql);
        statement.setInt(1, id);
        statement.setString(2, name);
        statement.setString(3, type);
        statement.setDouble(4, credit);
        statement.setString(5, semester);
        statement.executeUpdate();
        statement.close();
    }

    public void deleteCourse(int id) throws SQLException {
        String sql = "DELETE FROM course WHERE 课程编码 =?";
        PreparedStatement statement = connection.prepareStatement(sql);
        statement.setInt(1, id);
        statement.executeUpdate();
        statement.close();
    }

    public void updateCourse(int id, String name, String type, double credit, String semester) throws SQLException {
        String sql = "UPDATE course SET 课程名称 =?, 课程类型 =?, 课程学分 =?, 开始学期 =? WHERE 课程编码 =?";
        PreparedStatement statement = connection.prepareStatement(sql);
        statement.setString(1, name);
        statement.setString(2, type);
        statement.setDouble(3, credit);
        statement.setString(4, semester);
        statement.setInt(5, id);
        statement.executeUpdate();
        statement.close();
    }

    public void selectCourse(int id) throws SQLException {
        String sql = "SELECT * FROM course WHERE 课程编码 =?";
        PreparedStatement statement = connection.prepareStatement(sql);
        statement.setInt(1, id);
        ResultSet resultSet = statement.executeQuery();
        while (resultSet.next()) {
            int courseId = resultSet.getInt("课程编码");
            String courseName = resultSet.getString("课程名称");
            String courseType = resultSet.getString("课程类型");
            double courseCredit = resultSet.getDouble("课程学分");
            String courseSemester = resultSet.getString("开始学期");
            System.out.println("课程编码: " + courseId + ", 课程名称: " + courseName + ", 课程类型: " + courseType +
                    ", 课程学分: " + courseCredit + ", 开始学期: " + courseSemester);
        }
        resultSet.close();
        statement.close();
    }
}